package com.wkck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.wkck.VO.CheckUserDeliveredVo;
import com.wkck.context.BaseContext;
import com.wkck.domain.*;
import com.wkck.dto.UserDeliverResumeInfoDto;
import com.wkck.exception.BaseException;
import com.wkck.mapper.*;
import com.wkck.service.LResumeDeliveredService;
import com.wkck.utils.DateTimeGeneration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;

/**
 * @Author: CK
 * @Date: 2024/03/15/23:50
 * @Description:
 */
@Service
@Slf4j
public class LResumeDeliveredServiceImpl implements LResumeDeliveredService {

    @Autowired
    private JobJoinMapper jjMapper;

    @Autowired
    private DeliveredLibraryMapper deliverMapper;

    @Autowired
    private DeliveredLibraryHistoryMapper historyMapper;

    @Autowired
    private LResumeMapper resumeMapper;

    @Autowired
    private LResumeFilesMapper filesMapper;

    /***
     * @Author: CK
     * @Date: 2024/3/15
     * @Param: [dto]
     * @Description: 用户投递简历
     */
    @Override
    @Transactional
    public Boolean userDeliverResume(UserDeliverResumeInfoDto dto) {

        if(Objects.isNull(dto)) {
            throw new BaseException("参数异常 投递简历失败！");
        }

        if (dto.getUser_id() != BaseContext.getCurrentId().intValue()) {
            throw new BaseException("用户信息不一致 投递简历失败！");
        }

        LambdaQueryWrapper<Job_join> jjWrapper = new LambdaQueryWrapper<>();
        jjWrapper
                .eq(Job_join::getJob_id, dto.getCareer_id());
        Job_join jobJoin = jjMapper.selectOne(jjWrapper);
        if (Objects.isNull(jobJoin)) {
            throw new BaseException("职位信息为空 投递简历失败！");
        }

        Integer jjId = jobJoin.getJid();

        String[] commands = dto.getResumeChooseItem().split("\\.");

        if (commands.length != 2) {
            throw new BaseException("指令解析异常 投递简历失败！");
        }

        if (commands[1].equals("online")) {
            LambdaQueryWrapper<LResume> onlineWrapper = new LambdaQueryWrapper<>();
            onlineWrapper
                    .eq(LResume::getId, commands[0]);
            LResume onlineResume = resumeMapper.selectOne(onlineWrapper);

            if (Objects.isNull(onlineResume)) {
                throw new BaseException("用户简历信息为空 投递简历失败！");
            }
        } else if(commands[1].equals("file")) {
            LambdaQueryWrapper<LResumeFiles> filesWrapper = new LambdaQueryWrapper<>();
            filesWrapper
                    .eq(LResumeFiles::getId, commands[0]);
            LResumeFiles fileResume = filesMapper.selectOne(filesWrapper);

            if (Objects.isNull(fileResume)) {
                throw new BaseException("用户简历信息为空 投递简历失败！");
            }
        } else {
            throw new BaseException("未知的类型 投递简历失败！");
        }

        Delivered_Library build = Delivered_Library.builder()
                .job_join_id(jjId)
                .user_id(dto.getUser_id())
                .resume_id(Long.parseLong(commands[0]))
                .resume_type(commands[1])
                .audit_status(1)
                .deliver_time(DateTimeGeneration.now())
                .build();

        LambdaQueryWrapper<Delivered_Library> deliverWrapper = new LambdaQueryWrapper<>();
        deliverWrapper
                .eq(Delivered_Library::getJob_join_id, build.getJob_join_id())
                .eq(Delivered_Library::getUser_id, build.getUser_id());

        Delivered_Library deliveredLibrary = deliverMapper.selectOne(deliverWrapper);

        if (Objects.isNull(deliveredLibrary)) {
            Long id = IdWorker.getId();
            build.setId(id);
            deliverMapper.insert(build);

            // 得到简历投递id 插入到投递简历历史数据表中....
            DeliveredLibraryHistory history = DeliveredLibraryHistory.builder()
                    .id(IdWorker.getId())
                    .create_time(DateTimeGeneration.now())
                    .state_content("1")
                    .remark(null)
                    .dl_id(id)
                    .build();

            historyMapper.insertDeliverHistory(history);

            return true;
        } else {
            throw new BaseException("简历已投递 无需重复投递!");
        }
    }

    /***
     * @Author: CK
     * @Date: 2024/3/16
     * @Param: [career_id, user_id]
     * @Description: 检查用户是否在该职位已投递过简历
     */
    @Override
    public CheckUserDeliveredVo checkUserDeliveredResumeForCareer(Long careerId, Integer userId) {

        if (careerId == null || userId == null) {
            throw new BaseException("无法获取状态！");
        }

        return deliverMapper.checkUserDeliveredResumeForCareer(careerId, userId);
    }
}
